package cn.wuyu.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.core.content.ContextCompat;
import androidx.core.widget.TextViewCompat;
import cn.wuyu.baselibrary.R;
import cn.wuyu.baselibrary.app.SettingConfig;


/**
 * Created by xuzongmeng on 2018/1/10.
 */

public class ThemeUtil {
    public static int getColor(@ColorRes int id) {
        return ContextCompat.getColor(AppUtil.getAppContext(), id);
    }

    public static Drawable getDrawable(@DrawableRes int id) {
        return ContextCompat.getDrawable(AppUtil.getAppContext(), id);
    }

    public static void setTextAppearance(TextView tv, int textAppearance) {
        TextViewCompat.setTextAppearance(tv, textAppearance);
//        return ContextCompat.getDrawable(.getContext(), id);
    }

    //    public static int getThemeColor(Resources.Theme theme, int id){
//        TypedArray array = theme.obtainStyledAttributes(new int[]{id});
//        return array.getColor(0,0xFFFFFF);
//    }
    public static int getThemeColor(Resources.Theme theme, int id) {
        TypedArray array = theme.obtainStyledAttributes(new int[]{id});
        int color = array.getColor(0, 0xFFFFFF);
        array.recycle();
        return color;
    }

    public static int getAttrImg(int attrId) {
        int[] attrs = new int[]{attrId};
        TypedArray typedArray = AppUtil.getAppContext().obtainStyledAttributes(attrs);
        int imgRes = typedArray.getResourceId(0, 0);
        typedArray.recycle();
        return imgRes;
    }

    public static int getAttrColor(int attrId) {
        int[] attrs = new int[]{attrId};
        TypedArray typedArray = AppUtil.getAppContext().obtainStyledAttributes(attrs);
        int color = typedArray.getColor(0, 0);
        typedArray.recycle();
        return color;
    }

    public static int getAttrImg(Context context, int attrId) {
        int[] attrs = new int[]{attrId};
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int res = typedArray.getResourceId(0, 0);
        typedArray.recycle();
        return res;
    }

    public static int getAttrColor(Context context, int attrId) {
        int[] attrs = new int[]{attrId};
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int res = typedArray.getColor(0, 0);
        typedArray.recycle();
        return res;
    }


    public static Drawable getThemeDrawable(Resources.Theme theme, int id) {
        TypedArray array = theme.obtainStyledAttributes(new int[]{id});
        return array.getDrawable(0);
    }

    public static void changTheme(Activity activity) {
        activity.finish();
        final Intent intent = activity.getIntent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
    }

    public static void reCreate(@NonNull final Activity activity) {
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                activity.recreate();
//            }
//        });

        HandlerUtils.handler.post(new Runnable() {

            @Override
            public void run() {
                activity.recreate();
            }

        });

    }

    public static boolean configThemeBeforeOnCreate(@NonNull Activity activity, @StyleRes int dark, @StyleRes int light) {
        boolean isEnableBlack = SettingConfig.isDarkMode();
        activity.setTheme(isEnableBlack ? dark : light);
        return isEnableBlack;
    }



    public static void setTheme(@NonNull Activity activity) {
        boolean isDarkMode = SettingConfig.isDarkMode();
        activity.setTheme(isDarkMode ?  R.style.AppBlackTheme: R.style.AppWhiteTheme);
    }

}
